$(function () {
    var addressForm;
    var userId;
    var th;

    $("#choose").click(function () {
        addressForm = $("#addressForm");
        userId = localStorage.getItem("userId");
        th="";
        $.ajax({
            url : "easybuy/address/viewByuserId",
            data : {"userId": userId},
            dataType : "json",
            type : "post",
            success : function (data) {
                th = "<tr><td>选择</td><td>电话号码</td><td>收货地址</td><td>收货人姓名</td><td>操作</td>";
                addressForm.append(th);
                for(var i=0;i<data.length;i++){
                    addressForm.append(
                        "<tr><td><input type='radio'  style='zoom: 150%;' name='xx' onclick='show("+data[i].id+")'></td>" +
                        "<td>"+data[i].phoneNumber+"</td>"+
                        "<td>"+data[i].address+"</td><td>"+data[i].userName+"</td>"+
                        "<td><a href='Member_Address.html' style=\"color:#ff4e00;\">地址管理</a></td></tr>"
                    );
                }
            },
            error : function (XMLHttpRequest, textStatus, errorThrown) {
                //alert("出现错误");
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }

        });
    });
    viewDefault();
    function  viewDefault() {
        var userName = $("#userName");
        var loginName =$("#loginName");
        loginName.text(localStorage.getItem("user"));
        var userId = localStorage.getItem("userId");
        var remark = $("#remark");
        var phoneNumber = $("#phoneNumber");
        var postCode = $("#postCode");
        var address = $("#address");
        $.ajax({
            url : "easybuy/address/viewdefault",
            data : {"userId": userId},
            dataType : "json",
            type : "post",
            success : function (data) {
                if(data !=null){
                    userName.text(data.userName);
                    remark.text(data.remark);
                    phoneNumber.text(data.phoneNumber);
                    postCode.text(trans(data.postCode));
                    address.text(data.address);
                }
            },
            error : function (XMLHttpRequest, textStatus, errorThrown) {
                //alert("出现错误");
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }


        });
    }

});
    function show(id) {
        var userName = $("#userName");
        var loginName =$("#loginName");
        loginName.text(localStorage.getItem("user"));
        var remark = $("#remark");
        var phoneNumber = $("#phoneNumber");
        var postCode = $("#postCode");
        var address = $("#address");
        $.ajax({
            url : "easybuy/address/viewById",
            dataType : "json",
            data : {"ID":id},
            type : "post",
            success : function (data) {
                if(data !=null){
                    //alert("11");
                    userName.text(data.userName);
                    remark.text(data.remark);
                    phoneNumber.text(data.phoneNumber);
                    postCode.text(trans(data.postCode));
                    address.text(data.address);
                }
            },
            error : function (XMLHttpRequest, textStatus, errorThrown) {
                //alert("出现错误");
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }
        });
    }
function trans(postCode){
        if(postCode==null || postCode.length==0){
            return "";
        }else {
            return postCode;
        }
}